package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.SupplierImpl;
import com.shisfish.library.Model.Supplier;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("supplier")
public class SupplierController extends GlobalVariable {

	private SupplierImpl supplierImpl = new SupplierImpl();
	
	@POST
	@Path("insertASupplier/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertASupplier(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("name") String name, @FormParam("linkname") String linkname, 
			@FormParam("phone") String phone, @FormParam("email") String email, 
			@FormParam("remarks") String remarks) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			Supplier supplier = new Supplier(name, linkname, phone, email, remarks);
			
			int flag = supplierImpl.insertOne(supplier);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateASupplier/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateASupplier(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("name") String name, @FormParam("linkname") String linkname, 
			@FormParam("phone") String phone, @FormParam("email") String email, 
			@FormParam("remarks") String remarks, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Supplier> suppliers = supplierImpl.selectById(id);
			
			if (suppliers.size() > 0) {
				Supplier supplier = suppliers.get(0);
				supplier.setName(name);
				supplier.setLinkname(linkname);
				supplier.setEmail(email);
				supplier.setPhone(phone);
				supplier.setRemarks(remarks);
				int flag = supplierImpl.updateOne(supplier);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("deleteASurpplier/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteASurpplier(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			int flag = supplierImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, UPDATE_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAllPage/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = supplierImpl.countAll();
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize); 
			List<Supplier> suppliers = supplierImpl.selectAllPage(currentPage, pageSize);
			if (suppliers.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", suppliers);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectById/{id}")
	@Produces("application/json; charset=utf-8")
	public String selectById(@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Supplier> suppliers = supplierImpl.selectById(id);
		if (suppliers.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("obj", suppliers.get(0));
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	
	
}
